home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ivbsrc / access.frm next >
Text File  |  1995-05-08  |  7KB  |  238 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   3480
  5.    ClientLeft      =   90
  6.    ClientTop       =   1350
  7.    ClientWidth     =   4995
  8.    Height          =   3885
  9.    Left            =   30
  10.    LinkMode        =   1  'Source
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   3480
  13.    ScaleWidth      =   4995
  14.    Top             =   1005
  15.    Width           =   5115
  16.    Begin CommandButton Command0 
  17.       Caption         =   "Create a test file on disk C:"
  18.       Height          =   375
  19.       Left            =   120
  20.       TabIndex        =   7
  21.       Top             =   3000
  22.       Width           =   3375
  23.    End
  24.    Begin CommandButton Command6 
  25.       Caption         =   "Quit"
  26.       Height          =   855
  27.       Left            =   3600
  28.       TabIndex        =   4
  29.       Top             =   2520
  30.       Width           =   1335
  31.    End
  32.    Begin CommandButton Command5 
  33.       Caption         =   "Clear array"
  34.       Height          =   375
  35.       Left            =   3600
  36.       TabIndex        =   3
  37.       Top             =   2040
  38.       Width           =   1335
  39.    End
  40.    Begin CommandButton Command4 
  41.       Caption         =   "Clear list box"
  42.       Height          =   375
  43.       Left            =   3600
  44.       TabIndex        =   6
  45.       Top             =   1560
  46.       Width           =   1335
  47.    End
  48.    Begin CommandButton Command3 
  49.       Caption         =   "Rewrite array"
  50.       Height          =   375
  51.       Left            =   3600
  52.       TabIndex        =   5
  53.       Top             =   1080
  54.       Width           =   1335
  55.    End
  56.    Begin CommandButton Command2 
  57.       Caption         =   "Load list box"
  58.       Height          =   375
  59.       Left            =   3600
  60.       TabIndex        =   2
  61.       Top             =   600
  62.       Width           =   1335
  63.    End
  64.    Begin CommandButton Command1 
  65.       Caption         =   "Create array"
  66.       Height          =   375
  67.       Left            =   3600
  68.       TabIndex        =   1
  69.       Top             =   120
  70.       Width           =   1335
  71.    End
  72.    Begin ListBox List1 
  73.       Height          =   2760
  74.       Left            =   120
  75.       TabIndex        =   0
  76.       Top             =   120
  77.       Width           =   3375
  78.    End
  79. End
  80. Declare Function hread Lib "kernel" Alias "_hread" (ByVal hFile%, ByVal memAddr&, ByVal dwBytes&) As Long
  81.  
  82. Declare Function lclose Lib "Kernel" Alias "_lclose" (ByVal hFile As Integer) As Integer
  83. Declare Function llseek Lib "Kernel" Alias "_llseek" (ByVal hFile As Integer, ByVal lOffset As Long, ByVal iOrigin As Integer) As Long
  84.  
  85. Declare Function OpenFile Lib "Kernel" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Integer) As Integer
  86.  
  87. '  OpenFile() Flags
  88.  
  89. Const OF_READ = &H0
  90. Const OF_WRITE = &H1
  91. Const OF_CREATE = &H1000
  92.  
  93. Declare Function GlobalAlloc Lib "Kernel" (ByVal wFlags As Integer, ByVal dwBytes As Long) As Integer
  94. Declare Function GlobalFree Lib "Kernel" (ByVal hMem As Integer) As Integer
  95. Declare Function GlobalLock Lib "Kernel" (ByVal hMem As Integer) As Long
  96. Declare Function GlobalUnlock Lib "Kernel" (ByVal hMem As Integer) As Integer
  97. Declare Function GlobalHandleToSel Lib "Toolhelp.dll" (ByVal hglb As Integer) As Integer
  98. Declare Function MemoryRead Lib "Toolhelp.dll" (ByVal wSel As Integer, ByVal dwOffset As Long, lpvBuf As Any, ByVal dwcb As Long) As Long
  99. Declare Function MemoryWrite Lib "Toolhelp.dll" (ByVal wSel As Integer, ByVal dwOffset As Long, lpvBuf As Any, ByVal dwcb As Long) As Long
  100.  
  101. Const GMEM_MOVEABLE = &H2
  102. Const GMEM_ZEROINIT = &H40
  103. Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
  104.  
  105. Dim f_Of As OFSTRUCT        'Open file structure record
  106. Dim f_File$                 'Name of file containing sample records
  107. Dim f_NbrRecs As Long       'Number of records in sample file
  108. Dim f_mHndl As Integer      'Memory handle to global memory
  109. Dim f_Rec As f_RecType      'Sample record
  110. Dim f_mSel%                 'Memory selector
  111. Dim f_LenRec&               'Length of sample record
  112.  
  113. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
  114. Declare Function GetFocus% Lib "User" ()
  115. Const WM_USER = &H400
  116. Const LB_RESETCONTENT = (WM_USER + 5)
  117.  
  118. Sub Command0_Click ()
  119.  
  120. 'Write a series of records to a disk file. We'll read this file
  121. 'into memory later.
  122.  
  123.   Open "Sample.dat" For Random As #1 Len = Len(f_Rec)
  124.   For I& = 0 To 255
  125.     f_Rec.I = I&
  126.     f_Rec.L = I& * 2
  127.     f_Rec.C = I& * 3
  128.     f_Rec.S = I& / 10
  129.     f_Rec.D = I& / 100
  130.     f_Rec.ST = String$(30, I&)
  131.     Put 1, , f_Rec
  132.   Next I&
  133.   Close #1
  134. End Sub
  135.  
  136. Sub Command1_Click ()
  137.   Call CreateHuge
  138. End Sub
  139.  
  140. Sub Command2_Click ()
  141.   Call FillListBox
  142. End Sub
  143.  
  144. Sub Command3_Click ()
  145.   For I& = 255 To 0 Step -1  'Write records to memory in reverse order
  146.     f_Rec.I = I&
  147.     f_Rec.L = I& * 2
  148.     f_Rec.C = I& * 3
  149.     f_Rec.S = I& / 10
  150.     f_Rec.D = I& / 100
  151.     f_Rec.ST = String$(30, I&)
  152.     J& = 255& - I&
  153.     rBytes& = MemoryWrite(f_mSel%, J& * f_LenRec&, f_Rec, f_LenRec&)
  154.   Next I&
  155. End Sub
  156.  
  157. Sub Command4_Click ()
  158.   List1.SetFocus
  159.   ret& = SendMessage(GetFocus(), LB_RESETCONTENT, 0, 0&)
  160. End Sub
  161.  
  162. Sub Command5_Click ()
  163.   Ok% = GlobalFree(f_mHndl)
  164. End Sub
  165.  
  166. Sub Command6_Click ()
  167.   End
  168. End Sub
  169.  
  170. Sub CreateHuge ()
  171. '--- creates huge array of records from sample file. The records are in a
  172. '    type structure defined as "f_RecType".
  173.   
  174.   f_File$ = "Sample.dat"
  175.   
  176.   '--- open the data file for reading
  177.   hFile = OpenFile(f_File$, f_Of, OF_READ)
  178.  
  179.   '--- get the size of the file
  180.   size& = llseek(hFile, 0&, 2)
  181.         
  182.   '--- determine how many records are in the file
  183.   f_NbrRecs = size& \ Len(f_Rec)
  184.         
  185.   '--- reset the file pointer to the start of the file
  186.   rs& = llseek(hFile, 0&, 0)
  187.         
  188.   '--- create the global memory object
  189.   f_mHndl = GlobalAlloc(GHND, size&)
  190.  
  191.   '--- make sure enough memory is available
  192.   If f_mHndl = 0 Then
  193.     Beep
  194.     MsgBox "Insufficient memory to allocate array", 16, ""
  195.     Exit Sub
  196.   End If
  197.  
  198.   '--- get the address of the memory object
  199.   lpAddr& = GlobalLock(f_mHndl)
  200.  
  201.   '--- read the data file into the memory object
  202.   inBytes& = hread(hFile, ByVal lpAddr&, size&)
  203.         
  204.   '--- close the file
  205.   cl = lclose(hFile)
  206.  
  207.   '--- unlock the memory object
  208.   e = GlobalUnlock(f_mHndl)
  209.  
  210. End Sub
  211.  
  212. Sub FillListBox ()
  213.     
  214. '--- get the array memory object selector
  215. '=================================================================
  216. '    this only needs to be done once in any form or routine.
  217. '    Note that memory is NOT locked. It doesn't need to be in
  218. '    in protected mode so the selector is valid even if the memory
  219. '    object gets moved.  As this routine requires the Win 3.1 API
  220. '    calls, the app will always be running in protected mode.
  221. '=================================================================
  222.     
  223.   f_mSel% = GlobalHandleToSel(f_mHndl)
  224.     
  225.   f_LenRec& = Len(f_Rec)
  226.  
  227.   '--- read records from array (f_NbrRecs is total # of records)
  228.   For L& = 0 To f_NbrRecs - 1
  229.         
  230.     '--- read a record from array into f_Rec record structure
  231.     rBytes& = MemoryRead(f_mSel%, L& * f_LenRec&, f_Rec, f_LenRec&)
  232.     '--- add record to listbox
  233.     T$ = Str$(f_Rec.I) + Str$(f_Rec.L) + Str$(f_Rec.C) + Str$(f_Rec.S) + Str$(f_Rec.D) + "  " + f_Rec.ST
  234.     List1.AddItem T$
  235.   Next L&
  236. End Sub
  237.  
  238.